package 标准接口;



import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


public class CreateRequestinfo2120 {

//第一步做一次就行了  记录下来
    /**
     * 模拟缓存服务
     */
    private static final Map<String,String> SYSTEM_CACHE = new HashMap <>();

    /**
     * ecology 系统发放的授权许可证(appid)
     */
    private static final String APPID = "EEAA5436-7577-4BE0-8C6C-89E9D44801EA";


    public static void main(String[] args) {
        Date now=new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
        String dateString = formatter.format(now);

        String   tttttttttt="243fc分隔符fc费诚,14002fc分隔符fc杨成进";
           tttttttttt="1640fc分隔符fc王之鑫,5213fc分隔符fc豆育飞,22436fc分隔符fc姜志强,2293fc分隔符fc史晨晨,6205fc分隔符fc胡浩楠,17387fc分隔符fc赵贝宁,20782fc分隔符fc徐鸿源,20917fc分隔符fc张健龙,5650fc分隔符fc马宇,14484fc分隔符fc郭萌,2128fc分隔符fc何琪琦,11904fc分隔符fc孟德钊,11586fc分隔符fc柴涛,8436fc分隔符fc黄献平,22206fc分隔符fc陈梦珠,5048fc分隔符fc秦广,3876fc分隔符fc许璐,6609fc分隔符fc徐帅,1785fc分隔符fc程洋,5309fc分隔符fc徐啸,9791fc分隔符fc张久阳,4505fc分隔符fc褚玉壮,5080fc分隔符fc邓鸿磊,2746fc分隔符fc郭丰,3408fc分隔符fc梁俊峰,6274fc分隔符fc李昌,5014fc分隔符fc丁晨,3289fc分隔符fc张国明,18531fc分隔符fc聂志鹏,18490fc分隔符fc李非,22564fc分隔符fc章家瑜,8846fc分隔符fc杨志伟,12614fc分隔符fc秦何,10275fc分隔符fc贾森源,5355fc分隔符fc李科,11025fc分隔符fc陶晶晶";

        String lll[]=tttttttttt.split(",");
        for(int i=0;i<lll.length;i++){
            String xxxx[]=lll[i].split("fc分隔符fc");
            String userid=xxxx[0];
            String name=xxxx[1];
            String riqi="2025-01-08";
            HttpRequest.closeCookie();
            testRestful("http://oa.maxwell-gp.com.cn:8090","/api/workflow/paService/doCreateRequest",userid,name,riqi);
        }


    }
    /**
     * 第一步：
     *
     * 调用 ecology 注册接口,根据 appid 进行注册,将返回服务端公钥和 Secret 信息
     */

    public static Map<String,Object> testGetoken(String address){
        // 从系统缓存或者数据库中获取 ECOLOGY 系统公钥和 Secret 信息

        String secret ="6a393956-42c1-4b75-8b77-91c342ed90f1";
        String spk = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCmIqeh3HlAxMNSjepuY1YsRj6pqfX/r88v1xOKI7+vIXDpMLgmaA8ytRGTyHaxscXiXxcCKE6T+z54qrxaxw6b4/WSfcH8QaghHhQRAtvMWxP/SC1Vg5dniQMUVvrsB2wbf0DfyiftGeBf8gJbe1uWsXrMZLZE3olrDMCjylo8SwIDAQAB";

        SYSTEM_CACHE.put("SERVER_PUBLIC_KEY",spk);
        //ECOLOGY 返回的系统密钥
        SYSTEM_CACHE.put("SERVER_SECRET",secret);

        RSA rsa = new RSA(null,spk);
        //对秘钥进行加密传输，防止篡改数据
        String encryptSecret = rsa.encryptBase64(secret,CharsetUtil.CHARSET_UTF_8,KeyType.PublicKey);
        //调用 ECOLOGY 系统接口进行注册
        String data = HttpRequest.post(address+ "/api/ec/dev/auth/applytoken")
                .header("appid",APPID)
                .header("secret",encryptSecret)
                .header("time","3600")
                .execute().body();
        System.out.println("testGetoken()："+data);
        Map<String,Object> datas = JSONUtil.parseObj(data);
        //ECOLOGY 返回的 token
        // TODO 为 Token 缓存设置过期时间
        SYSTEM_CACHE.put("SERVER_TOKEN",StrUtil.nullToEmpty((String)datas.get("token")));
        return datas;
    }

    public static String testRestful(String address,String api,String userid,String name,String riqi ){

        //ECOLOGY 返回的 token
        String token=(String) testGetoken(address).get("token");
        String spk = SYSTEM_CACHE.get("SERVER_PUBLIC_KEY");

        //封装请求头参数
        RSA rsa = new RSA(null,spk);
        //对用户信息进行加密传输,暂仅支持传输 OA 用户 ID
        String encryptUserid = rsa.encryptBase64("1",CharsetUtil.CHARSET_UTF_8,KeyType.PublicKey);
        //调用 ECOLOGY 系统接口


        //System.out.println(dateString);
        String requestName="零部件厂商课程馈表-"+name+"-"+riqi;


        String data = HttpRequest.post(address + api)
                .header("appid",APPID)
                .header("token",token)
                .header("userid",encryptUserid)
                .body("workflowId=2120&requestName="+requestName+"&"
                                +"mainData = ["
                                + "{'fieldName':'applyid','fieldValue':'"+userid+"'},"
                                + "]"

                )

                .execute().body();

        System.out.println("token:"+token);
        System.out.println("userid:"+encryptUserid);
        System.out.println("testRestful()："+data);

        return data;
    }



}
